{{extend 'layout.html'}}
<script type="text/javascript" src="/{{=request.application}}/static/js/jquery.timeago.js"></script>
<style> 
input[type="checkbox"] {
vertical-align: text-bottom;
}
.menu, .menu + td { border: 1px solid black}
</style> 

<script>
function filter(){
        $('#events tr').hide()
        $('.access:checked').each(function(){
            name1 = $(this).attr('name')                
            $('.table:checked').each(function(){
                name2 = $(this).attr('name')
                $('#events tr').each(function(){
                    if ($(this).is(name1+name2)){
                        var text = $('input[name=keyword]').val().toLowerCase().replace(/[^a-zA-Z 0-9]+/g,'');
                        if (text != '') var pattern = new RegExp('^.*' + text + '.*$', 'ig');
                        else var pattern1 = new RegExp('.*', 'ig');
                        
                        if($.trim($(this).text()).match(pattern))
                            $(this).show()
                    }
                })                    
            })                  
        })
}
$(function(){
    $('.timestamp').timeago()                 
    $('input[name=keyword]').keyup(function(){ filter()})
    $('input[type=checkbox]').click(function(){ filter()})
})    
</script>
<table>
<tr><td>Logo</td></tr>
<tr>
        <td width="15%" class='menu'>
            <table class='menu_table' width='100%'>
                {{=LOAD('default','sidebar.load')}}
            </table>        
        </td>
<td>
<table>{{=TR(TD(LABEL('Filter by keywords:')),TD(INPUT(_name='keyword',_placeholder='Enter a keyword')))}}</table>
<table>
<tr>
{{=TD(LABEL('Filter by access:'))}}
{{=TD(INPUT(_type="checkbox",_name=".create",_class='access',value=True),'create')}}
{{=TD(INPUT(_type="checkbox",_name=".update",_class='access',value=True),'update')}}
{{=TD(INPUT(_type="checkbox",_name=".delete",_class='access',value=True),'delete')}}
</tr>
</table>
<table>
<tr>
{{=TD(LABEL('Filter by type:'))}}
{{=TD(INPUT(_type="checkbox",_name=".msg",_class='table',value=True),'messages')}}
{{=TD(INPUT(_type="checkbox",_name=".auth_user",_class='table',value=True),'users')}}
{{=TD(INPUT(_type="checkbox",_name=".msg_tag",_class='table',value=True),'tagging')}}
{{=TD(INPUT(_type="checkbox",_name=".auth_membership",_class='table',value=True),'memberships')}}
{{=TD(INPUT(_type="checkbox",_name=".contact",_class='table',value=True),'contacts')}}
</tr>
<tr>
<td></td>
{{=TD(INPUT(_type="checkbox",_name=".auth_group",_class='table',value=True),'groups')}}
{{=TD(INPUT(_type="checkbox",_name=".tag",_class='table',value=True),'tags')}}
{{=TD(INPUT(_type="checkbox",_name=".msg_group",_class='table',value=True),'groupings')}}
{{=TD(INPUT(_type="checkbox",_name=".msg_attachment",_class='table',value=True),'attachments')}}
</tr>
</table>
<hr/>
{{if page:}}
<div style="float: left;">
<a href="{{=URL(args=[page-1])}}">[previous {{=items_per_page}} events]</a>
</div>
{{pass}}
{{if len(evnts)>items_per_page:}}
<div style="float: right; width: 100px;">
<a href="{{=URL(args=[page+1])}}">[next {{=items_per_page}} events]</a>
</div>
{{pass}}
<br/>

<table id='events'>
{{for evnt in evnts:}}
<tr class="{{=evnt['table']}} {{=evnt['access']}}">
<td>
{{if evnt['table'] == 'tag':}}
    {{if evnt['access'] == 'create':}}
        {{=evnt['user']}} added a new tag {{=A(evnt['item'].name, _href=URL(c='tags')) if evnt['item'] else evnt['details'] + ' ' + '[was deleted]'}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
    {{elif evnt['access'] == 'update':}}
        {{=evnt['user']}} updated the tag {{=A(evnt['item'].name, _href=URL(c='tags')) if evnt['item'] else '[was deleted]'}} [{{=evnt['details']}}]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> 
        {{elif evnt['access'] == 'delete':}}
{{=evnt['user']}} deleted the tag {{=evnt['details']}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> </td>
    {{pass}}

{{elif evnt['table'] == 'auth_group':}}
    {{if evnt['access'] == 'create':}}
        {{=evnt['user']}} added a new group {{=A(evnt['item'].role, _href=URL(c='groups')) if evnt['item'] else evnt['details'] + ' ' + '[was deleted]'}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
    {{elif evnt['access'] == 'update':}}
        {{=evnt['user']}} updated the group {{=A(evnt['item'].role, _href=URL(c='groups')) if evnt['item'] else '[was deleted]'}} [{{=evnt['details']}}]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> 
    {{elif evnt['access'] == 'delete':}}
        {{=evnt['user']}} deleted the group {{=evnt['details']}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> </td>
    {{pass}}

{{elif evnt['table'] == 'contact':}}
    {{if evnt['access'] == 'create':}}
        {{type,info = evnt['details'].split(',')}}
        {{=evnt['user']}} added a new {{=type}} {{=info}} for contact {{=A(evnt['item'].contact_info, _href=URL(c='contacts')) if evnt['item'] else '[was deleted]'}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
    {{elif evnt['access'] == 'update':}}
        {{=evnt['user']}} updated the contact {{=A(evnt['item'].contact_info, _href=URL(c='contacts')) if evnt['item'] else '[was deleted]'}} [{{=evnt['details']}}]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> 
    {{elif evnt['access'] == 'delete':}}
        {{type,info = evnt['details'].split(',')}}
        {{=evnt['user']}} deleted the {{=type}} {{=info}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> </td>
    {{pass}}

{{elif evnt['table'] == 'auth_user':}}
    {{if evnt['access'] == 'create':}}
        {{=evnt['user']}} added a new user {{=A(evnt['item'].first_name + ' ' + evnt['item'].last_name, _href=URL('users','read', args=evnt['item'].id)) if evnt['item'] else '[was deleted]'}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
    {{elif evnt['access'] == 'update':}}
        {{=evnt['user']}} updated the user {{=A(evnt['item'].first_name + ' ' + evnt['item'].last_name, _href=URL('users','read', args=evnt['item'].id)) if evnt['item'] else '[was deleted]'}} [{{=evnt['details']}}]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> 
    {{elif evnt['access'] == 'delete':}}
        {{=evnt['user']}} deleted the user {{=evnt['details']}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> </td>
    {{elif evnt['access'] == 'change password':}}        
        {{=evnt['user']}} changed the password for user {{=A(evnt['item'].first_name + ' ' + evnt['item'].last_name, _href=URL('users','read', args=evnt['item'].id)) if evnt['item'] else '[was deleted]'}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span> </td>
    
    {{pass}}

{{elif evnt['table'] == 'msg':}}
	{{name = evnt['item'].created_by.user_id.first_name + ' ' + evnt['item'].created_by.user_id.last_name}}
    {{if evnt['access'] == 'create':}}
        {{if evnt['item'].parent_msg == 0:}}
            {{=evnt['user']}} added a new message {{=A(evnt['item'].subject, _href=URL('messages','read', args=evnt['item'].id))}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{else:}}    
            {{=evnt['user']}} replied to the message {{=A(evnt['item'].parent_msg.subject, _href=URL('messages','read', args=evnt['item'].parent_msg.id))}} by {{=name}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>        
        {{pass}}
    {{elif evnt['access'] == 'delete':}}
        {{if evnt['item'].parent_msg == 0:}}
            {{=evnt['user']}} deleted the message {{=A(evnt['item'].subject, _href=URL('messages','read', args=evnt['item'].id))}} by {{=name}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{else:}}    
            {{=evnt['user']}} deleted the reply {{=A(evnt['item'].subject, _href=URL('messages','read', args=evnt['item'].id))}} by {{=name}} of message {{=A(evnt['item'].parent_msg.subject, _href=URL('messages','read',args=evnt['item'].parent_msg.id))}} by {{=evnt['item'].parent_msg.created_by.user_id.first_name  + ' ' + evnt['item'].parent_msg.created_by.user_id.last_name}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>        
        {{pass}}
    {{pass}}

{{elif evnt['table'] == 'msg_tag':}}
    {{if evnt['access'] == 'create':}}
        {{if evnt['item']:}}
            {{=evnt['user']}} tagged the message {{=A(evnt['item'].msg_id.subject, _href=URL('messages','read', args=evnt['item'].id))}} as {{=A(evnt['item'].tag_id.name, _href=URL('tags','index'))}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{else:}}
            {{tag = evnt['details'].split(',')[1]}}
            {{subject,msg_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}
            {{=evnt['user']}} tagged the message {{=A(subject,_href=URL('messages','read', args=msg_id))}} as {{=tag}} [Already removed]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{pass}}
    {{elif evnt['access'] == 'delete':}}
        {{tag = evnt['details'].split(',')[1]}}
        {{subject,msg_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}
        {{=evnt['user']}} removed the tag {{=A(tag, _href=URL('tags'))}} in the message {{=A(subject,_href=URL('messages','read', args=msg_id))}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>    
    {{pass}}    

{{elif evnt['table'] == 'msg_group':}}
    {{if evnt['access'] == 'create':}}
        {{if evnt['item']:}}
            {{=evnt['user']}} grouped the message {{=A(evnt['item'].msg_id.subject, _href=URL('messages','read', args=evnt['item'].id))}} to {{=A(evnt['item'].group_id.role, _href=URL('groups'))}} group. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{else:}}
            {{group = evnt['details'].split(',')[1]}}
            {{subject,msg_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}
            {{=evnt['user']}} grouped the message {{=A(subject,_href=URL('messages','read', args=msg_id))}} to {{=A(group, _href=URL('groups'))}} group [Already removed]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{pass}}
    {{elif evnt['access'] == 'delete':}}
        {{group = evnt['details'].split(',')[1]}}
        {{subject,msg_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}
        {{=evnt['user']}} removed the message {{=A(subject,_href=URL('messages','read', args=msg_id))}} from the {{=A(group, _href='groups')}} group. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
    {{pass}}    

{{elif evnt['table'] == 'auth_membership':}}
    {{group = evnt['details'].split(',')[1]}}
    {{email,user_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}

    {{if evnt['access'] == 'create':}}
        {{if evnt['item']:}}
            {{=evnt['user']}} added the user {{=A(evnt['item'].user_id.first_name + ' ' + evnt['item'].user_id.last_name, _href=URL('users','read', args=evnt['item'].user_id.id))}} to {{=A(evnt['item'].group_id.role, _href=URL(c='groups'))}} group. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{else:}}
            {{=evnt['user']}} added the user {{=A(email, _href=URL('users','read', args=user_id))}} to {{=A(group, _href=URL(c='groups'))}} group [Already removed]. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{pass}}
    {{elif evnt['access'] == 'delete':}}
        {{group = evnt['details'].split(',')[1]}}
        {{email,user_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}
        {{=evnt['user']}} removed the user {{=A(email, _href=URL('users','read', args=user_id))}} from the {{=A(group, _href=URL(c='groups'))}} group. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>        
    {{pass}}    

{{elif evnt['table'] == 'msg_attachment':}}
    {{filename = evnt['details'].split(',')[1]}}
    {{subject,msg_id = evnt['details'].split(',')[0],evnt['details'].split(',')[2]}}

    {{if evnt['access'] == 'create':}}
        {{if evnt['item']:}}
            {{=evnt['user']}} attach the file {{=A(filename, _href=URL('default','download', args=evnt['item'].attachment))}} to the message  {{=A(evnt['item'].msg_id.subject, _href=URL('messages','read', args=msg_id))}} by {{=evnt['item'].msg_id.created_by.user_id.first_name + ' ' +  evnt['item'].msg_id.created_by.user_id.last_name}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        {{else:}}
            {{=evnt['user']}} attach the file {{=filename + ' [already deleted]'}} to the message {{=A(subject, _href=URL('messages','read', args=msg_id))}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>

        {{pass}}
    {{elif evnt['access'] == 'delete':}}
            {{=evnt['user']}} deleted the file {{=filename}} attached to the message {{=A(subject, _href=URL('messages','read', args=msg_id))}}. <span class='timestamp' title={{=evnt['timestamp'].isoformat()}}></span>
        
    {{pass}}    
{{pass}}
</td>
</tr>
{{pass}}
</table>
<br/>
{{if page:}}
<div style="float: left;">
<a href="{{=URL(args=[page-1])}}">[previous {{=items_per_page}} events]</a>
</div>
{{pass}}
{{if len(evnts)>items_per_page:}}
<div style="float: right; width: 100px;">
<a href="{{=URL(args=[page+1])}}">[next {{=items_per_page}} events]</a>
</div>
{{pass}}

</td></tr>
</table>
